package com.fractalwrench.harmonybootstrap.sample;

import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading.H1;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading.H2;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading.H3;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading.H4;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading.H5;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading.H6;

import com.beardedhen.harmonybootstrap.BootstrapLabel;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

public class BootstrapLabelExample extends AbilitySlice implements Component.ClickedListener {

    private BootstrapLabel changeColor,changeHeading,changeRound;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(com.fractalwrench.harmonybootstrap.sample.ResourceTable.Layout_example_bootstrap_label);

        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_example_bootstrap_label,null,false);

        changeColor = (BootstrapLabel)rootLayout.findComponentById(ResourceTable.Id_example_blabel_change_color);
        changeHeading = (BootstrapLabel)rootLayout.findComponentById(ResourceTable.Id_example_blabel_change_heading);
        changeRound = (BootstrapLabel)rootLayout.findComponentById(ResourceTable.Id_example_blabel_change_rounded);

        changeColor.setClickedListener(this);
        changeHeading.setClickedListener(this);
        changeRound.setClickedListener(this);

        super.setUIContent(rootLayout);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_example_blabel_change_color:
                try {
                    onColorChangeClicked();
                } catch (NotExistException e) {
                    e.printStackTrace();
                } catch (WrongTypeException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case ResourceTable.Id_example_blabel_change_heading:
                try {
                    onHeadingChangeClicked();
                } catch (NotExistException e) {
                    e.printStackTrace();
                } catch (WrongTypeException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case ResourceTable.Id_example_blabel_change_rounded:
                try {
                    changeRound.setRounded(!changeRound.isRounded());
                } catch (NotExistException e) {
                    e.printStackTrace();
                } catch (WrongTypeException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    private  void onHeadingChangeClicked() throws NotExistException, WrongTypeException, IOException {
        switch ((DefaultBootstrapHeading) changeHeading.getBootstrapHeading()) {
            case H1:
                changeHeading.setBootstrapHeading(H2);
                break;
            case H2:
                changeHeading.setBootstrapHeading(H3);
                break;
            case H3:
                changeHeading.setBootstrapHeading(H4);
                break;
            case H4:
                changeHeading.setBootstrapHeading(H5);
                break;
            case H5:
                changeHeading.setBootstrapHeading(H6);
                break;
            case H6:
                changeHeading.setBootstrapHeading(H1);
                break;
            default:
                break;
        }
    }

    private void onColorChangeClicked() throws NotExistException, WrongTypeException, IOException {
        switch ((DefaultBootstrapBrand) changeColor.getBootstrapBrand()) {
            case PRIMARY:
                changeColor.setBootstrapBrand(DefaultBootstrapBrand.SUCCESS);
                break;
            case SUCCESS:
                changeColor.setBootstrapBrand(DefaultBootstrapBrand.INFO);
                break;
            case INFO:
                changeColor.setBootstrapBrand(DefaultBootstrapBrand.WARNING);
                break;
            case WARNING:
                changeColor.setBootstrapBrand(DefaultBootstrapBrand.DANGER);
                break;
            case DANGER:
                changeColor.setBootstrapBrand(DefaultBootstrapBrand.SECONDARY);
                break;
            case SECONDARY:
                changeColor.setBootstrapBrand(DefaultBootstrapBrand.REGULAR);
                break;
            case REGULAR:
                changeColor.setBootstrapBrand(DefaultBootstrapBrand.PRIMARY);
                break;
            default:
                break;
        }
    }
}
